home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16986 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: druid.borland.com!usenet
  2. From: pete@borland.com (Pete Becker)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Help! Macro replacement......
  5. Date: 13 Apr 1996 01:03:07 GMT
  6. Organization: Borland International
  7. Message-ID: <4kmugb$r1a@druid.borland.com>
  8. References: <4kega2$1tk@hptemp1.cc.umr.edu>
  9. NNTP-Posting-Host: pbecker.borland.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=ISO-8859-1
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <4kega2$1tk@hptemp1.cc.umr.edu>, jlu@cs.umr.edu says...
  15. >
  16. >
  17. >Hi *,
  18. >
  19. >I wrote an ugly macro definition which, when I compiled, surprised
  20. >me. The macro is defined as
  21. >
  22. >   #define ABS(X) ((X <= 0) ? -X : X)
  23. >
  24. >And I called the macro by using
  25. >
  26. >   int y = ABS(-6+4);
  27. >
  28. >I expected y would be 10. However, Solaris CC and Turbo C++ version 3.0
  29. >for DOS returned compilation error. And IRIX CC and g++ version 2.6.3 
  30. >and 2.7.0 returned 10.
  31. >
  32. >The problem is clear that one replacement gives
  33. >
  34. >   int y = --6+4;    // compilation error
  35. >
  36. >And the other gives
  37. >
  38. >   int y = - -6+4; // y = 10
  39. >
  40. >I tried to find out the answer from Chap 16 of ANSI C++ standard 
  41. >draft but cannot figure out which one is correct. Will someone 
  42. >please EMAIL me which one conforms to ANSI C++? If interested, I 
  43. >can post the summary. Thanks!
  44.  
  45. Better yet, I can tell you how to write that macro so that it works with all 
  46. compilers. Just put parentheses around each X in the macro definition:
  47.  
  48.    #define ABS(X) (((X) <= 0) ? -(X) : (X))
  49.  
  50.